﻿using AutoMapper;
using Basic.Traceability.Domain.Products;
using Basic.Traceability.ErrorCode;
using Basic.Traceability.Host.Api.Read.Application.Command.Products.TraceableProducts;
using Basic.Traceability.Host.Api.Read.Dto;
using Basic.Traceability.Intrestructrue.Base;
using MediatR;

namespace Basic.Traceability.Host.Api.Read.Application.Handler.Products.TraceableProducts
{
    public class GetTraceProductsHandler : IRequestHandler<GetTraceProductsCommand, APIResult<List<TraceableProductsDto>>>
    {
        private readonly IBaseRepository<TraceableProductsModel> traceProductsRepository;
        private readonly IMapper mapper;
        private readonly ILogger<GetTraceProductsHandler> logger;

        public GetTraceProductsHandler(IBaseRepository<TraceableProductsModel> traceProductsRepository, IMapper mapper, ILogger<GetTraceProductsHandler> logger)
        {
            this.traceProductsRepository = traceProductsRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        /// <summary>
        /// 查询产品基本信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<TraceableProductsDto>>> Handle(GetTraceProductsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<TraceableProductsDto>> res = new APIResult<List<TraceableProductsDto>>();
            var model = traceProductsRepository.GetModel(request.Id);
            var data = mapper.Map<List<TraceableProductsDto>>(model);
            res.Msg = "查询产品基本信息成功";
            res.Data = data;
            res.Code = APIEnums.Success;
            logger.LogInformation("查询产品基本信息成功");
            return Task.FromResult(res);
        }
    }
}
